package com.smart.community.parking.controller;

import com.smart.community.commons.result.PageResult;
import com.smart.community.commons.result.Result;
import com.smart.community.commons.utils.SecurityUtils;
import com.smart.community.parking.dto.ParkingLotDTO;
import com.smart.community.parking.dto.ParkingLotQueryDTO;
import com.smart.community.parking.service.IParkingLotService;
import com.smart.community.parking.vo.ParkingLotStatusVO;
import com.smart.community.parking.vo.ParkingLotVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 停车场管理控制器
 * 
 * @author Wu.Liang
 * @since 2024-12-21
 */
@Slf4j
@RestController
@RequestMapping("/parking/lots")
@Tag(name = "停车场管理", description = "停车场管理相关接口")
@Validated
public class ParkingLotController {

    @Autowired
    private IParkingLotService parkingLotService;

    /**
     * 分页查询停车场列表
     * 
     * @author Wu.Liang
     */
    @GetMapping
    @Operation(summary = "分页查询停车场列表")
    public Result<PageResult<ParkingLotVO>> getParkingLotList(ParkingLotQueryDTO queryDTO) {
        try {
            // 获取当前用户信息（通过用户上下文获取，无需验证Token）
            Long userId = SecurityUtils.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            PageResult<ParkingLotVO> result = parkingLotService.getParkingLotList(queryDTO, userId);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("查询停车场列表失败", e);
            return Result.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询停车场
     * 
     * @author Wu.Liang
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询停车场")
    public Result<ParkingLotVO> getParkingLot(@PathVariable Long id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            ParkingLotVO result = parkingLotService.getParkingLot(id, userId);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("查询停车场失败", e);
            return Result.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 新增停车场
     * 
     * @author Wu.Liang
     */
    @PostMapping
    @Operation(summary = "新增停车场")
    public Result<String> createParkingLot(@RequestBody @Valid ParkingLotDTO parkingLotDTO) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            boolean success = parkingLotService.createParkingLot(parkingLotDTO, userId);
            if (success) {
                return Result.success("创建成功");
            } else {
                return Result.fail("创建失败");
            }
        } catch (Exception e) {
            log.error("创建停车场失败", e);
            return Result.fail("创建失败：" + e.getMessage());
        }
    }

    /**
     * 更新停车场
     * 
     * @author Wu.Liang
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新停车场")
    public Result<String> updateParkingLot(@PathVariable Long id, @RequestBody @Valid ParkingLotDTO parkingLotDTO) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            boolean success = parkingLotService.updateParkingLot(id, parkingLotDTO, userId);
            return success ? Result.success("更新成功") : Result.fail("更新失败");
        } catch (Exception e) {
            log.error("更新停车场失败", e);
            return Result.fail("更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除停车场
     * 
     * @author Wu.Liang
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除停车场")
    public Result<String> deleteParkingLot(@PathVariable Long id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            boolean success = parkingLotService.deleteParkingLot(id, userId);
            return success ? Result.success("删除成功") : Result.fail("删除失败");
        } catch (Exception e) {
            log.error("删除停车场失败", e);
            return Result.fail("删除失败：" + e.getMessage());
        }
    }

    /**
     * 获取停车场实时状态
     * 
     * @author Wu.Liang
     */
    @GetMapping("/{id}/status")
    @Operation(summary = "获取停车场实时状态")
    public Result<ParkingLotStatusVO> getParkingLotStatus(@PathVariable Long id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            ParkingLotStatusVO result = parkingLotService.getParkingLotStatus(id, userId);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("获取停车场状态失败", e);
            return Result.fail("查询失败：" + e.getMessage());
        }
    }
} 